home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1998 July / EnigmA AMIGA RUN 29 (1998)(G.R. Edizioni)(IT)[!][issue 1998-07 & 08].iso / earcd / phase5 / ppcrelease / examples / msg2old.c < prev    next >
C/C++ Source or Header  |  1998-02-21  |  9KB  |  245 lines

  1. #include <exec/types.h>
  2. #include <exec/nodes.h>
  3. #include <exec/lists.h>
  4. #include <exec/memory.h>
  5. #include <utility/tagitem.h>
  6. #include <powerup/ppclib/interface.h>
  7. #include <powerup/ppclib/message.h>
  8. #include <powerup/ppclib/tasks.h>
  9. #include <powerup/proto/ppc.h>
  10. #include <proto/exec.h>
  11. #include <proto/dos.h>
  12. #include <proto/timer.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "time.h"
  16. #include "time_protos.h"
  17.  
  18. #define TEXT            "Text sent by M68k processor\n"
  19. #define KILL_ID         0x4b494c4c
  20.  
  21. extern struct Library    *SysBase;
  22.  
  23.  
  24. int    main(void)
  25. {
  26. struct Library        *PPCLibBase;
  27. struct Library        *TimerBase;
  28. struct MsgPort        *timermp;
  29. struct timerequest    *timerio;
  30. struct TagItem        MyTags[10];
  31. void            *PPCPort;
  32. void            *M68kPort;
  33. void            *ReplyPort;
  34. void            *StartupMsg;
  35. void            *M68kMsg;
  36. void            *PPCMsg;
  37. void            *Object;
  38. void            *Task;
  39. UBYTE            *Body;
  40. void            *MyTimerObject;
  41.  
  42.   printf("Opening ppc.library\n");
  43.   if (PPCLibBase = OpenLibrary("ppc.library", 44))
  44.   {
  45.     if (MyTimerObject=TimerCreateObject())
  46.     {
  47.       if ((timermp = CreatePort(NULL, 0)) != NULL)
  48.       {
  49.         if ((timerio = (struct timerequest*) CreateStdIO(timermp)) != NULL)
  50.         {
  51.           if (OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *)timerio, 0) == 0L)
  52.           {
  53.             TimerBase    =    (struct Library*) timerio->tr_node.io_Device;
  54.  
  55.             printf("Loading PPC object\n");
  56.             Object    =    PPCLoadObject("PROGDIR:Msg2oldPPC.elf");
  57.  
  58.             printf("Creating reply port\n");
  59.             MyTags[0].ti_Tag    =    TAG_DONE;
  60.             if (ReplyPort = PPCCreatePort(MyTags))
  61.             {
  62.               if (StartupMsg = PPCCreateMessage(ReplyPort, 0))
  63.               {
  64.                 printf("Creating PPC task\n");
  65.                 MyTags[0].ti_Tag    =    PPCTASKTAG_STARTUP_MSG;
  66.                 MyTags[0].ti_Data    =(ULONG) StartupMsg;
  67.  
  68.                 MyTags[1].ti_Tag    =    PPCTASKTAG_STARTUP_MSGDATA;
  69.                 MyTags[1].ti_Data    =    NULL;
  70.  
  71.                 MyTags[2].ti_Tag    =    PPCTASKTAG_STARTUP_MSGLENGTH;
  72.                 MyTags[2].ti_Data    =    0;
  73.  
  74.                 MyTags[3].ti_Tag    =    PPCTASKTAG_STARTUP_MSGID;
  75.                 MyTags[3].ti_Data    =    0;
  76.  
  77.                 MyTags[4].ti_Tag    =    TAG_DONE;
  78.  
  79.                 if (Task = PPCCreateTask(Object, MyTags))
  80.                 {
  81.                   printf("Allocating memory for message body\n");
  82.                   if (Body = PPCAllocVec(sizeof(TEXT), MEMF_PUBLIC))
  83.                   {
  84.                     printf("Creating message port...");
  85.                     MyTags[0].ti_Tag    =    PPCPORTTAG_NAME;
  86.                     MyTags[0].ti_Data    =(ULONG) "M68k port";
  87.                     MyTags[1].ti_Tag    =    TAG_DONE;
  88.  
  89.                     TimerSetAttr(MyTimerObject,TIMERTAG_START);
  90.                     if (M68kPort = PPCCreatePort(MyTags))
  91.                     {
  92.                       TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  93.                       TimerShow(MyTimerObject);
  94.                       printf("Creating message...");
  95.                       TimerSetAttr(MyTimerObject,TIMERTAG_START);
  96.                       if (M68kMsg = PPCCreateMessage(ReplyPort, sizeof(TEXT)))
  97.                       {
  98.                         TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  99.                         TimerShow(MyTimerObject);
  100.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  101.                         printf("Obtaining PPC port...");
  102.                         MyTags[0].ti_Tag    =    PPCPORTTAG_NAME;
  103.                         MyTags[0].ti_Data    =(ULONG) "PPC port";
  104.                         MyTags[1].ti_Tag    =    TAG_DONE;
  105.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  106.  
  107.                         while (!(PPCPort = PPCObtainPort(MyTags)));
  108.  
  109.                         TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  110.                         TimerShow(MyTimerObject);
  111.  
  112.                         printf("Sending message...");
  113.                         strcpy(Body, TEXT);
  114.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  115.                         PPCSendMessage(PPCPort,
  116.                                        M68kMsg,
  117.                                        Body,
  118.                                        sizeof(TEXT),
  119.                                        0x12345678);
  120.                         TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  121.                         TimerShow(MyTimerObject);
  122.  
  123.                         printf("Waiting for reply...");
  124.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  125.                         PPCWaitPort(ReplyPort);
  126.                         TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  127.                         TimerShow(MyTimerObject);
  128.  
  129.                         printf("Waiting for PPC message...");
  130.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  131.                         PPCWaitPort(M68kPort);
  132.                         TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  133.                         TimerShow(MyTimerObject);
  134.  
  135.                         printf("Getting message...");
  136.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  137.                         if (PPCMsg = PPCGetMessage(M68kPort))
  138.                         {
  139.                           TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  140.                           TimerShow(MyTimerObject);
  141.                           Printf("Message: %s", (char *) PPCGetMessageAttr(PPCMsg, PPCMSGTAG_DATA));
  142.                           printf("Reply message...");
  143.                           TimerSetAttr(MyTimerObject,TIMERTAG_START);
  144.                           PPCReplyMessage(PPCMsg);
  145.                           TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  146.                           TimerShow(MyTimerObject);
  147.                         }
  148.                         else
  149.                         {
  150.                           Printf("Did not get PPC msg\n");
  151.                         }
  152.  
  153.                         printf("Releasing PPC port...");
  154.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  155.                         PPCReleasePort(PPCPort);
  156.                         TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  157.                         TimerShow(MyTimerObject);
  158.  
  159.                         printf("Waiting for Task Finish Msg...\n");
  160.                         for (;;)
  161.                         {
  162.                           if ((M68kMsg=PPCGetMessage(ReplyPort)) == StartupMsg)
  163.                           {
  164.                             printf("Ahh..the expected Startup=Finish Msg was received.\nNow we can savely free all resources.\n");
  165.                             break;
  166.                           }
  167.                           else
  168.                           {
  169.                             printf("Some non replied Msg 0x%lx was found..wait\n",
  170.                                    M68kMsg);
  171.                             PPCWaitPort(ReplyPort);
  172.                           }
  173.                         }
  174.  
  175.                         printf("Deleting message...");
  176.                         TimerSetAttr(MyTimerObject,TIMERTAG_START);
  177.                         PPCDeleteMessage(M68kMsg);
  178.                         TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  179.                         TimerShow(MyTimerObject);
  180.                       }
  181.                       else
  182.                       {
  183.                         Printf("Could not create msg\n");
  184.                       }
  185.                       printf("Deleting message port...");
  186.                       TimerSetAttr(MyTimerObject,TIMERTAG_START);
  187.                       while (PPCDeletePort(M68kPort) == FALSE);
  188.                       TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  189.                       TimerShow(MyTimerObject);
  190.                     }
  191.                     else
  192.                     {
  193.                       Printf("Could not create M68k msg port\n");
  194.                     }
  195.                     printf("Deleting reply port...");
  196.                     TimerSetAttr(MyTimerObject,TIMERTAG_START);
  197.                     while (PPCDeletePort(ReplyPort) == FALSE);
  198.                     TimerSetAttr(MyTimerObject,TIMERTAG_STOP);
  199.                     TimerShow(MyTimerObject);
  200.                   }
  201.                   else
  202.                   {
  203.                     Printf("Could not alloc mem for msg body\n");
  204.                   }
  205.                   printf("Freeing message body memory\n");
  206.                   PPCFreeVec(Body);
  207.                 }
  208.                 else
  209.                 {
  210.                   Printf("Could not create PPC task\n");
  211.                 }
  212.               }
  213.               else
  214.               {
  215.                 Printf("Could not create PPC message\n");
  216.               }
  217.             }
  218.             else
  219.             {
  220.               Printf("Could not create reply port\n");
  221.             }
  222.             printf("Unloading PPC object\n");
  223.             PPCUnLoadObject(Object);
  224.             CloseDevice((struct IORequest *)timerio);
  225.           }
  226.           DeleteStdIO((struct IOStdReq *)timerio);
  227.         }
  228.         DeletePort(timermp);
  229.       }
  230.       else
  231.       {
  232.         Printf("Could not load the elfobject\n");
  233.       }
  234.       TimerDeleteObject(MyTimerObject);
  235.     }
  236.     printf("Closing ppc.library\n");
  237.     CloseLibrary(PPCLibBase);
  238.   }
  239.   else
  240.   {
  241.     Printf("Could not open ppc.library v44+\n");
  242.   }
  243. }
  244.  
  245.